package com.itdct.onflow.system.common.manager

import com.itdct.onflow.core.extend.deleteBatchEx
import com.itdct.onflow.core.extend.logInfo
import com.itdct.onflow.system.common.base.BaseManager
import com.itdct.onflow.system.file.entity.SysFileEntity
import com.itdct.onflow.system.file.manager.upload.BaseFileTransfer
import com.itdct.onflow.system.file.mapper.SysFileMapper
import org.springframework.stereotype.Component
import java.util.*

/**
 * @author Zhouwx
 * @date 2025/9/22 10:43:48
 * @version 1.0
 * @description
 */
@Component
class CleanExpireFileManager(
    val sysFileMapper: SysFileMapper,
    val baseFileTransfer: BaseFileTransfer
) : BaseManager() {
    fun cleanExpireFile() {
        logInfo("开始清理过期文件")
        val lowGradeFileKeepTime = onflowConfig.file.lowGradeFileKeepTime
        val calendar = Calendar.getInstance()
        calendar.add(Calendar.DAY_OF_YEAR, -lowGradeFileKeepTime)
        val expireDate = calendar.time
        val sysFileEntities: List<SysFileEntity> = sysFileMapper.listExpireFiles(expireDate, Date())
        for (sysFileEntity in sysFileEntities) {
            if (sysFileEntity.fileGrade == 20) {
                // INFO: Zhouwx: 2025/9/25 高等级文件不允许删除
                continue
            }
            baseFileTransfer.delete(sysFileEntity)
            logInfo("删除过期文件：${sysFileEntity.fileName}，存储路径为：${sysFileEntity.storePath}")
        }
        sysFileMapper.deleteBatchEx(sysFileEntities, 0L)
        logInfo("删除过期文件完成，共删除${sysFileEntities.size}个文件")
    }
}